home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Russian / cpsimage.cab / data / sys / unit.elf < prev    next >
Text File  |  2009-04-23  |  5KB  |  171 lines

  1. /*
  2. ** $Id: unit.elf,v 1.6 2008/10/03 20:20:53 campanel Exp $
  3. */
  4.  
  5. #load "sys/lang.elf";
  6.  
  7.  
  8. /******************************************************************************/
  9. /*
  10. ** This class provides unit testing to the language.
  11. */
  12. /* @assertTrue Asserts a value is true. */
  13. /* @assertFalse Asserts a value is true. */
  14. /* @assertNull Asserts a value is null. */
  15. /* @assertNotNull Asserts a value is not null. */
  16. /* @assertEqual Asserts two values are equal. */
  17. /* @assertNotEqual Asserts two values are not equal. */
  18. /******************************************************************************/
  19. private
  20. CLASS UNITTEST {
  21.     INTEGER FailedAsserts = 0;
  22.     INTEGER Asserts = 0;
  23.  
  24.     METHOD assertTrue( BOOLEAN cond, STRING msg )
  25.     {
  26.         this.Asserts++;
  27.  
  28.         if( !cond ) {
  29.             this.fail( cond:"not TRUE", msg:msg );
  30.         }
  31.     }
  32.  
  33.     METHOD assertFalse( BOOLEAN cond, STRING msg )
  34.     {
  35.         this.Asserts++;
  36.  
  37.         if( cond ) {
  38.             this.fail( cond:"not FALSE", msg:msg );
  39.         }
  40.     }
  41.  
  42.     METHOD assertNull( OBJECT obj, STRING msg )
  43.     {
  44.         this.Asserts++;
  45.  
  46.         if( obj ) {
  47.             this.fail( cond:"not NULL", msg:msg );
  48.         }
  49.     }
  50.  
  51.     METHOD assertNotNull( OBJECT obj, STRING msg )
  52.     {
  53.         this.Asserts++;
  54.  
  55.         if( !obj ) {
  56.             this.fail( cond:"is NULL", msg:msg );
  57.         }
  58.     }
  59.  
  60.     METHOD assertEqual( OBJECT exp, OBJECT act, STRING msg )
  61.     {
  62.         this.Asserts++;
  63.  
  64.         if( (!act && !exp) || (exp && (act == exp)) ) {
  65.             return;
  66.         }
  67.  
  68.         this.fail( cond:"expected " + exp + " but got " + act, msg:msg );
  69.     }
  70.  
  71.     METHOD assertNotEqual( OBJECT exp, OBJECT act, STRING msg )
  72.     {
  73.         this.Asserts++;
  74.  
  75.         if( (!act && !exp) || (exp && (act == exp)) ) {
  76.             this.fail( cond:"is EQUAL", msg:msg );
  77.         }
  78.     }
  79.  
  80.     METHOD fail( STRING cond, STRING msg )
  81.     {
  82.         CLASSREF cr = new( CLASSREF, handle:this );
  83.  
  84.         STRING fmt = "Test failed in " + cr.getName() + ", " + cond;
  85.         if( msg ) {
  86.             fmt = fmt + ": " + msg;
  87.         }
  88.         print fmt;
  89.         DEBUGGER.printStackTrace();
  90.         DEBUGGER.log( msg:fmt );
  91.         print "";
  92.         this.FailedAsserts++;
  93.     }
  94. }
  95.  
  96. /******************************************************************************/
  97. /*
  98. ** This executes all known unit tests.
  99. */
  100. /******************************************************************************/
  101. private
  102. PROCEDURE ExecUnitTests( BOOLEAN verbose, BOOLEAN noRun )
  103. {
  104.     INTEGER tests = 0;
  105.     INTEGER asserts = 0;
  106.     INTEGER failed = 0;
  107.     CLASSREF ut = REFLECTION.findClass( name:"UNITTEST" );
  108.  
  109.     CLASSREF cr;
  110.     OBJECT obj;
  111.     LIST methods;
  112.     BOOLEAN hassetup, hasteardown;
  113.  
  114.     /* Get all classes */
  115.     LIST classes = REFLECTION.listClasses();
  116.     foreach( classes : c ) {
  117.         cr = REFLECTION.findClass( name:c );
  118.  
  119.         /* Does it extend UNITTEST */
  120.         if( ut.isAssignableFrom( cls:cr ) ) {
  121.             methods = cr.listMethods();
  122.             hassetup = cr.hasMethod( name:"setUp" );
  123.             hasteardown = cr.hasMethod( name:"tearDown" );
  124.             obj = cr.newInstance();
  125.             foreach( methods : m ) {
  126.                 if( m.indexOf( match:"test" ) == 0 ) {
  127.                     if ( hassetup && verbose && noRun )
  128.                         print "Executing " + c + ".setUp";
  129.                     else if ( hassetup && verbose ) {
  130.                         print "Executing " + c + ".setUp";
  131.                         cr.invokeMethod( obj:obj, name:"setUp" );
  132.                     }
  133.                     else if ( hassetup )
  134.                         cr.invokeMethod( obj:obj, name:"setUp" );
  135.  
  136.  
  137.                     if ( verbose && noRun )
  138.                         print "Executing " + c + "." + m;
  139.                     else if ( verbose ) {
  140.                         print "Executing " + c + "." + m;
  141.                         cr.invokeMethod( obj:obj, name:m );
  142.                     }
  143.                     else
  144.                         cr.invokeMethod( obj:obj, name:m );
  145.  
  146.  
  147.                     if ( hasteardown && verbose && noRun )
  148.                         print "Executing " + c + ".tearDown";
  149.                     else if ( hasteardown && verbose ) {
  150.                         print "Executing " + c + ".tearDown";
  151.                         cr.invokeMethod( obj:obj, name:"tearDown" );
  152.                     }
  153.                     else if( hasteardown )
  154.                         cr.invokeMethod( obj:obj, name:"tearDown" );
  155.  
  156.                     tests++;
  157.                 }
  158.             }
  159.  
  160.             asserts = asserts + cr.getField( obj:obj, name:"Asserts" );
  161.             failed = failed + cr.getField( obj:obj, name:"FailedAsserts" );
  162.         }
  163.     }
  164.  
  165.     print "TOTAL Assertions " + asserts + ", Tests " + tests;
  166.     if( failed != 0 ) {
  167.         print "FAILED Assertions " + failed;
  168.         SetStatus( op:"Stop", msg:"Unit tests failed" );
  169.     }
  170. }
  171.